﻿using System.Collections.Generic;
using System.ComponentModel;
using Windows.Storage;

namespace Sidvall.UI.Uwp.Configuration
{
    public abstract class SettingsBase : Sidvall.Configuration.SettingsBase, INotifyPropertyChanged
    {
        #region Public Members

        #region GetSetting

        protected override T GetSetting<T>(string propertyName)
        {
            if (_Settings.Values.ContainsKey(propertyName))
                return (T)_Settings.Values[propertyName];
            return default(T);
        }

        #endregion
        #region SetSetting

        protected override bool SetSetting<T>(string propertyName, T value)
        {
            if (_Settings.Values.ContainsKey(propertyName))
            {
                var currentValue = (T)_Settings.Values[propertyName];
                if (EqualityComparer<T>.Default.Equals(currentValue, value))
                    return false;
            }

            _Settings.Values[propertyName] = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        #endregion

        #endregion
        #region Private Members

        private readonly ApplicationDataContainer _Settings;

        #endregion
        #region Constructors

        protected SettingsBase(ApplicationDataContainer settings)
        {
            _Settings = settings;
        }

        #endregion
    }
}
